import OriginAxios from 'axios';
import {messageNotice as message} from './utils';
const axios = OriginAxios.create({
  timeout: 20000,
});
class Myaxios extends axios {
  get(url, data) {
    return axios.get(url, {
      params: data,
    });
  }
}
export function get(url, data) {
  return axios.get(url, {
    params: data,
  });
}

//By default, axios serializes JavaScript objects to JSON
export function post(url, data) {
  return axios({
    url,
    method: 'post',
    data,
  });
}

//By default, axios serializes JavaScript objects to JSON
export function put(url, data) {
  return axios({
    url,
    method: 'put',
    data,
  });
}

//By default, axios serializes JavaScript objects to JSON
export function deleteAxios(url, data) {
  return axios({
    url,
    method: 'delete',
    data,
  });
}

// Add a request interceptor
axios.interceptors.request.use(
  function(config) {
    // Do something before request is sent
    return config;
  },
  function(error) {
    // Do something with request error
    return Promise.reject(error);
  },
);

//返回状态判断(添加响应拦截器)
axios.interceptors.response.use(
  res => {
    if (res.data && res.data.code) {
      let errorMsg = res.data.msgs;
      message.error(errorMsg);
      return Promise.reject(errorMsg);
    }
    return res.data.data;
  },
  error => {
    // 用户登录的时候会拿到一个基础信息,比如用户名,token,过期时间戳
    // 直接丢localStorage或者sessionStorage
    if (error.response.status == 500) {
      let errorMsg = error.response.data;
      message.error(errorMsg);
      return Promise.reject(errorMsg);
    }
  },
);

export default axios;
